package com.yrihr.wisdom.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yrihr.wisdom.dto.NewsDto;
import com.yrihr.wisdom.entity.News;
import com.yrihr.wisdom.service.NewsSercice;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author by Hasee
 * @Classname TestController
 * @Description  测试controller
 * @Date 2020/11/6 11:34
 */
@Slf4j
@RestController
@RequestMapping("/news")
@RequiredArgsConstructor
public class TestController {
    private final NewsSercice newsSercice;

    @PostMapping("/page")
    public IPage<News> getUserPage(Page<?> page, @RequestBody NewsDto news){
        return newsSercice.selectPageNews(page,news);
    }

    @RequestMapping("/pageList")
    public IPage<News> getNewsPageList(Page<?> page){
        return newsSercice.getNewsPageList(page);
    }

    /**
     * 测试count 方法，带条件查询条数，不带条件查询总条数
     */
    @RequestMapping("/qwer")
    public void aaa() throws ParseException {
        QueryWrapper<News> query = Wrappers.query();
        // 相同
        query.eq("newsCd","NEWS000646749");
        // 不同
        query.ne("pubDate","2020-11-04 00:00:00");
        int count = newsSercice.count(query);
        News news = new News();
        news.setId("NEWS000646749");
        news.setDetailUrl("http://ff.dayu.com/contents/386832471305488384?_fetch_author=1&biz_id=1001");
        news.setNewRecord(false);
        news.setNewsCd("NEWS000646749");
        news.setNewsNm("泰州市人大法制委督查《泰州市水环境保护条例》实施情况");
        news.setNewsRsn("大鱼号");
        news.setProCat("12");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        news.setPubDate(sdf.parse("2020-11-04 00:00:00"));
        news.setUpdateTime(new Date());
        System.out.println("count："+count);
        // 如果找到id=id  其他不同，即count=1，找到了不同，则需要更新
        // 如果找到id=id  其他全相同，即结果count=0,没有找到不同，则不更新
        if (count == 1) {
        boolean b = newsSercice.saveOrUpdate(news);
        System.out.println(b);
        }

    }

}
